/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.shop.entity.ExpressTools;
import com.thinkgem.jeesite.modules.shop.dao.ExpressToolsDao;
import com.thinkgem.jeesite.modules.shop.entity.ExpressPrice;
import com.thinkgem.jeesite.modules.shop.dao.ExpressPriceDao;

/**
 * 快递工具Service
 * @author xxs
 * @version 2016-08-31
 */
@Service
@Transactional(readOnly = true)
public class ExpressToolsService extends CrudService<ExpressToolsDao, ExpressTools> {

	@Autowired
	private ExpressPriceDao expressPriceDao;
	
	public ExpressTools get(String id) {
		ExpressTools expressTools = super.get(id);
		expressTools.setExpressPriceList(expressPriceDao.findList(new ExpressPrice(expressTools)));
		return expressTools;
	}
	
	public List<ExpressTools> findList(ExpressTools expressTools) {
		return super.findList(expressTools);
	}
	
	public Page<ExpressTools> findPage(Page<ExpressTools> page, ExpressTools expressTools) {
		return super.findPage(page, expressTools);
	}
	
	@Transactional(readOnly = false)
	public void save(ExpressTools expressTools) {
		super.save(expressTools);
		for (ExpressPrice expressPrice : expressTools.getExpressPriceList()){
			if (expressPrice.getId() == null){
				continue;
			}
			if (ExpressPrice.DEL_FLAG_NORMAL.equals(expressPrice.getDelFlag())){
				if (StringUtils.isBlank(expressPrice.getId())){
					expressPrice.setExpressTools(expressTools);
					expressPrice.preInsert();
					expressPriceDao.insert(expressPrice);
				}else{
					expressPrice.preUpdate();
					expressPriceDao.update(expressPrice);
				}
			}else{
				expressPriceDao.delete(expressPrice);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(ExpressTools expressTools) {
		super.delete(expressTools);
		expressPriceDao.delete(new ExpressPrice(expressTools));
	}
	
}